#include "SchmidtCAM.h"

int main( int argc, char *argv[] ){

/**** handle option parameters ****/
	int opt;
	opterr = 1;

	int n_row 			= 1250;	// default
	int n_col 			= 4700;	// default
	int fast_read_opt	= 0;
	int n_bytes			= 2;	// default 32bits float
	double t_exp		= 0.0;

	char fout_name[1024];
	int fname_set 		= -1;

	while( (opt=getopt(argc,argv,"r:c:f:o:b:t:")) != -1 ){
		switch(opt){
			case 'r':
				n_row = atoi(optarg);
				printf("> Num. of vertical transfers: %d\n", n_row);
				break;
			case 'c':
				n_col = atoi(optarg);
				printf("> Num. of horizontal transfers: %d\n", n_col);
				break;
			case 'f':
				fast_read_opt = atoi(optarg);
				break;
			case 'o':
				sprintf(fout_name, "%s", optarg);
				fname_set = 1;
				printf("> data will be saved into: %s\n", fout_name);
				break;
			case 'b':
				n_bytes = atoi(optarg);
				printf("(%s)> date format %d bytes (2 bytes: 16bits unsigned; 4 bytes: 32bits float).\n", __FUNCTION__, n_bytes);
				break;
			case 't':
				t_exp = atof(optarg);
				printf("> exposure time: %f seconds\n", t_exp);
				break;
			default:
				break;
		}
	}

	if( fname_set == -1 ){
		sprintf(fout_name, "tmp.fits");
	}

/**** memery map ****/

	int fd_gpio = open("/dev/mem", O_RDWR | O_SYNC);

	// ADDR_GPIO_OUT --> 0x41200000
	uint32_t *gpio_reg_ctrl = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd_gpio, ADDR_GPIO_OUT);

	// ADDR_GPIO_IN  --> 0x41210000
	uint32_t *gpio_reg_status = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd_gpio, ADDR_GPIO_IN);

	// ADDR_PL_PS_DDR_CNT --> 0x41210008
	uint32_t *gpio_reg_pl_ps_ddr_cnt = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd_gpio, ADDR_PL_PS_DDR_CNT);

/**** call ccd_exposure ****/

    int is_dark = 0;
	ccd_exposure(   t_exp, 
                    gpio_reg_ctrl, 
                    gpio_reg_status, 
                    gpio_reg_pl_ps_ddr_cnt, 
                    n_row,
                    n_col,
					n_bytes,
                    fast_read_opt,
                    is_dark,
                    fout_name,
					"",
					"" );

	munmap(gpio_reg_ctrl,10);
	munmap(gpio_reg_status,10);
	munmap(gpio_reg_pl_ps_ddr_cnt,10);
	close(fd_gpio);

	return 0;
}
